Skip to main content

Vectors and Matrix Operations

Numbers, Vectors, Matrices

Recall that the set of real numbers is R\mathbb{R} and that a vector, vRnv \in \mathbb{R}^n, is just an nn-tuple of numbers. Similarly, an n×mn \times m matrix is just a table of numbers, with nn rows and mm columns and we can write

AmnRmnA_{mn} \in \mathbb{R}^{mn}

Note that a vector is normally considered equivalent to a n×1n\times 1 matrix i.e. we view these as column vectors.

Examples

Example

In R, a vector can be generated with:

> X <- 3:6
> X
[1] 3 4 5 6

A matrix can be generated in R as follows,

> matrix(X)
[,1]
[1,] 3
[2,] 4
[3,] 5
[4,] 6
Note

We note that R distinguishes between vectors and matrices.

Elementary Operations

We can define multiplication of a real number kk and a vector v=(v1,,vn)v=(v_1,\ldots,v_n) by kv=(kv1,,kvn)k\cdot v=(kv_1,\ldots,kv_n). The sum of two vectors in Rn\mathbb{R}^n, v=(v1,,vn)v=(v_1,\ldots,v_n) and u=(u1,,un)u=(u_1,\ldots,u_n) is defined as the vector v+u=(v1+u1,,vn+un)v+u=(v_1+u_1,\ldots,v_n+u_n). We can define multiplication of a number and a matrix and the sum of two matrices (of the same sizes) similarly.

Examples

Example
> A <- matrix(c(1,2,3,4), nr=2, nc=2)

> A
[,1] [,2]
[1,] 1 3
[2,] 2 4

> B <- matrix(c(1,0,2,1), nr=2, nc=2)

> B
[,1] [,2]
[1,] 1 2
[2,] 0 1

> A+B
[,1] [,2]
[1,] 2 5
[2,] 2 5

The Tranpose of a Matrix

In R, matrices may be constructed using the matrix function and the transpose of AA, AA^\prime, may be obtained in R by using the t function:

> A <- matrix(1:6, nrow=3)

> t(A)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

Details

If AA is an n×mn \times m matrix with element aija_{ij} in row ii and column jj, then AA^\prime or ATA^T is the m×nm\times n matrix with element aija_{ij} in row jj and column ii.

Examples

Example

Consider a vector in R

> x <- 1:4

> x
[1] 1 2 3 4

> t(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4

> matrix(x)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
> t(matrix(x))
[,1] [,2] [,3] [,4]
Note

The first solution gives a 1×n1 \times n matrix and the second solution gives a n×1n \times 1 matrix.

Matrix Multiplication

Matrices AA and BB can be multiplied together if AA is an n×pn \times p matrix and BB is an p×mp\times m matrix. The general element cijc_{ij} of n×mn\times m, C=ABC=AB, is found by pairing the ithi^{th} row of CC with the jthj^{th} column of BB, and computing the sum of products of the paired terms.

Fig. 39

Details

Matrices AA and BB can be multiplied together if AA is a n×pn\times p matrix and BB is a p×mp\times m matrix. Given the general element cijc_{ij} of n×mn \times m matrix, C=ABC=AB is found by pairing the ithi^{th} row of CC with the jthj^{th} column of BB, and computing the sum of products of the paired terms.

Examples

Example: Matrices in R
> A <- matrix(c(1,3,5,2,4,6),3,2)

> A
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6

> B <- matrix(c(1,1,2,3),2,2)

> B
[,1] [,2]
[1,] 1 2
[2,] 1 3

> A%*%B
[,1] [,2]
[1,] 3 8
[2,] 7 18
[3,] 11 28

More on Matrix Multiplication

Let AA, BB, and CC be m×nm\times n, n×ln\times l, and l×pl\times p matrices, respectively. Then we have

(AB)C=A(BC)(AB)C=A(BC)

In general, matrix multiplication is not commutative, that is ABBAAB\neq BA.

We also have

(AB)=BA(AB)'=B'A'

In particular, (Av)(Av)=vAAv(Av)'(Av)=v'A'Av, when vv is a n×1n\times1 column vector

More obvious are the rules

  1. A+(B+C)=(A+B)+CA+(B+C)=(A+B)+C

  2. k(A+B)=kA+kBk(A+B)=kA+kB

  3. A(B+C)=AB+ACA(B+C)=AB+AC

where kRk\in\mathbb{R} and when the dimensions of the matrices fit.

Linear Equations

Details

General linear equations can be written in the form Ax=bAx=b.

Examples

Example

The set of equations

2x+3y=42x+3y=4

3x+y=23x+y=2

can be written in matrix formulation as

[2331][xy]=[42]\begin{bmatrix} 2 & 3 \\ 3 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 4 \\ 2 \end{bmatrix}

i.e. Ax=bA\underline{x} = \underline{b} for an appropriate choice of A,xA, \underline{x} and b\underline{b}.

The Unit Matrix

The n×nn\times n matrix

I=[10001000001]I = \left[ \begin{array}{cccc} 1 & 0 & \ldots & 0 \\ 0 & 1 & 0 & \vdots \\ \vdots & 0 & \dots & 0 \\ 0 & \ldots & 0 & 1 \end{array} \right]

is the identity matrix. This is because if a matrix AA is n×nn\times n

then AI=AA I = A and IA=AI A = A

The Inverse of a Matrix

If AA is an n×nn \times n matrix and BB is a matrix such that

BA=AB=IBA = AB = I

then BB is said to be the inverse of AA, written

B=A1B = A ^{-1}

Note that if AA is an n×nn \times n matrix for which an inverse exists, then the equation Ax=bAx = b can be solved and the solution is x=A1bx = A^{-1} b.

Examples

Example

If matrix AA is:

[2331]\begin{bmatrix} 2 & 3 \\ 3 & 1 \end{bmatrix}

then A1A ^{-1} is:

[17373727]\begin{bmatrix} \displaystyle\frac{-1}{7} & \displaystyle\frac{3}{7} \\ \displaystyle\frac{3}{7} & \displaystyle\frac{-2}{7} \end{bmatrix}